package com.banciyuan.bcywebview.biz.circles;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.CircleApi;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.ProgressbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.pulltorefresh.PullToRefreshBase;
import com.banciyuan.bcywebview.base.view.pulltorefresh.PullToRefreshListView;
import com.banciyuan.bcywebview.biz.circles.adapter.CircleUserAdapter;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.OnErrorProxy;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.banciyuan.bcywebview.utils.http.BcyPair;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.daoexample.model.ActiveUser;

/**
 * Created by squall on 2016/6/15.
 */
public class CircleMoreUserActivity extends BaseActivity {

    private PullToRefreshListView refreshListView;
    private ListView mListView;
    private CircleUserAdapter adapter;
    private ProgressbarHelper mProgressbarHelper;
    private View mProgressbarView;
    private RequestQueue queue;
    private List<ActiveUser> slist = new ArrayList<>();
    private ActionbarHelper mActionbarHelper;
    private View mActionBarView;
    private int page = 1;
    private boolean isEnd = false;
    private String type;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic_list);
        initArgs();
        initProgressbar();
        initActionbar();
        initUi();
        initAction();
        initData();
    }

    @Override
    protected void initArgs() {
        queue = VolleyQueue.getRquest(this);
        type = getIntent().getStringExtra(gotoUtil.INTENT_VALUE_TWO);
        id = getIntent().getStringExtra(gotoUtil.INTENT_VALUE_ONE);
    }

    @Override
    protected void initUi() {
        refreshListView = (PullToRefreshListView) findViewById(R.id.topic_refresh_lv);
        mListView = refreshListView.getRefreshableView();
    }

    @Override
    protected void initData() {
        List<BcyPair> paramsList = new ArrayList<BcyPair>();
        String url = HttpUtils.BASE_URL + CircleApi.getClassifyRank();
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
        paramsList.add(new BcyPair(HttpUtils.INDEX_P, String.valueOf(page)));
        paramsList.add(new BcyPair(HttpUtils.TYPE, type));
        paramsList.add(new BcyPair(HttpUtils.ID, id));
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String result) {
                if (StatusCodeUtils.dealStatusCode(result, CircleMoreUserActivity.this)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        JSONArray data = jsonObject.getJSONArray("data");
                        Gson gson = new Gson();
                        List<ActiveUser> temp = gson.fromJson(data.toString(), new TypeToken<List<ActiveUser>>() {
                        }.getType());
                        if (!temp.isEmpty()) {
                            dealJson(temp);
                        } else {
                            isEnd = true;
                        }

                    } catch (Exception e) {
                        mProgressbarHelper.onFailed();
                        refreshListView.onRefreshComplete();
                    }
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mProgressbarHelper.onFailed();
                refreshListView.onRefreshComplete();
            }
        };
        HashMap<String, String> map = HttpUtils.getData(paramsList);
        OnErrorProxy errorProxy = new OnErrorProxy(onError, onSucc, url, CircleMoreUserActivity.this, map);
        StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, errorProxy);
        queue.add(requestParam);
    }

    private void dealJson(List<ActiveUser> temp) {
        for (ActiveUser a : temp) {
            a.setType(type);
        }
        if (page == 1) {
            slist.clear();
        }
        slist.addAll(temp);
        for (int i = 0; i < 3; i++) {
            slist.get(i).setIndex(i + 1);
        }
        if (null != adapter) {
            adapter.notifyDataSetChanged();
        } else {
            adapter = new CircleUserAdapter(CircleMoreUserActivity.this, slist);
            mListView.setAdapter(adapter);
        }
        mProgressbarHelper.onSuccess();
        refreshListView.onRefreshComplete();
        isEnd = false;
    }

    @Override
    protected void initProgressbar() {
        mProgressbarView = findViewById(R.id.base_progressbar);
        mProgressbarHelper = new ProgressbarHelper(mProgressbarView);
        mProgressbarHelper.setActionCallbacks(new ProgressbarHelper.OnProgressCallbacks() {
            @Override
            public void onActionClick(int id) {
                mProgressbarHelper.onLoading();
                initData();
            }
        });
        mProgressbarHelper.onLoading();
    }

    @Override
    protected void initAction() {
        refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                initData();
            }
        });

        refreshListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                if (!isEnd) {
                    page++;
                    initData();
                }
            }
        });
    }

    @Override
    protected void initActionbar() {
        mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.high_producion));
    }
}
